﻿Imports Microsoft.Win32
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap


Module Program

    Private ReadOnly SETTINGS_PATH As String = Path.Combine(Environment.GetFolderPath( _
                    Environment.SpecialFolder.ApplicationData), "Sansa Image Converter")
    Private ReadOnly FILENAME As String = Path.Combine(SETTINGS_PATH, "Settings.xml")

    Public opts As AppSettings

    Public Sub Main(ByVal args() As String)
        Initialize()
        Application.EnableVisualStyles()
        Application.DoEvents()
        If args Is Nothing Or args.Length <> 1 Then
            Application.Run(New OptionsDialog)
        Else
            Dim dlgWork As New ProgressDialog()
            dlgWork.PathToConvert = args(0)
            dlgWork.ShowDialog()
        End If
    End Sub


    Sub Initialize()
        'Dim fltl As New Microsoft.VisualBasic.Logging.FileLogTraceListener
        'fltl.Location = Logging.LogFileLocation.Custom
        '1) Remove the default FileLogTraceListener via app.config, and Reconfigure My.Application.Log to use a different TraceListener.
        '2) Listen to Unhandled Exception event and continue when the event happens.
        '3) Try/catch every logging call (or at least the ones that could conceivably be the first to execute).
        With My.Application.Log.DefaultFileLogWriter
            .Append = True
            .AutoFlush = True
            .Delimiter = ";"
            .IncludeHostName = True
            .CustomLocation = SETTINGS_PATH
            '.Location = Logging.LogFileLocation.LocalUserApplicationDirectory
            .TraceOutputOptions = TraceOptions.Timestamp
            .LogFileCreationSchedule = Logging.LogFileCreationScheduleOption.Daily
            .MaxFileSize = 100000
        End With

        opts = New AppSettings()

        If Not Directory.Exists(opts.ExportPath) Then
            opts.ExportPath = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyPictures, "Sansa Image Converter")
        End If
        LoadAppSettings()
    End Sub

    Public Sub SaveAppSettings()
        If Not File.Exists(FILENAME) Then
            Directory.CreateDirectory(Path.GetDirectoryName(FILENAME))
        End If

        Dim s As New FileStream(FILENAME, FileMode.OpenOrCreate)
        Dim f As New SoapFormatter
        Try
            f.Serialize(s, Program.opts)
        Catch ex As SerializationException
            My.Application.Log.WriteException(ex)
            MsgBox("Failed to serialize. Reason: " & ex.Message)
        Finally
            s.Close()
        End Try
    End Sub

    Public Sub LoadAppSettings()
        If Not File.Exists(FILENAME) Then
            Directory.CreateDirectory(Path.GetDirectoryName(FILENAME))
            Exit Sub
        End If

        Dim s As New FileStream(FILENAME, FileMode.OpenOrCreate)
        Dim f As New SoapFormatter
        Try
            opts = DirectCast(f.Deserialize(s), AppSettings)
        Catch ex As SerializationException
            My.Application.Log.WriteException(ex)
            MsgBox("Failed to deserialize. Reason: " & ex.Message)
        Finally
            s.Close()
        End Try
    End Sub

End Module
